package edu.seu.mart.dao.impl;

import edu.seu.mart.dao.BaseDao;
import edu.seu.mart.dao.IOrderDao;
import edu.seu.mart.entity.Pager;
import edu.seu.mart.model.Order;
import org.springframework.stereotype.Repository;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

@Repository("orderDao")
public class OrderDao extends BaseDao<Order> implements IOrderDao {

//    @Override
//    public Pager<Order> loadBySellerId(int id) {
//        return this.find("from Order where seller_id = '" + id + "'");
//    }
//
//    @Override
//    public Pager<Order> loadByUserId(int id) {
//        return this.find("from Order where user_id = '" + id + "'");
//    }
//
//
//    @Override
//    public Pager<Order> findOrderByState(String state) {
//        return this.find("from Order where state = '" + state + "'");
//    }
//
//    @Override
//    public Pager<Order> findBySellState(int seller, String state) {
//        return this.find("from Order where seller_id = '" + seller + "' and state = '" + state + "'");
//    }
//
//    @Override
//    public Pager<Order> findOrderById(int id) {
//        return this.find("from Order where id = '"+ id +"'");
//    }
//
//    @Override
//    public Pager<Order> findByUserState(int user, String state) {
//        return this.find("from Order where user_id = '" + user + "' and state = '" + state + "'");
//    }
    @Override
    public Double getMoneyOfOneDayBySeller(Date time, int sellerId) {

        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Double sum = (Double) getSession().createSQLQuery("select sum(a.total) from t_order as a  where  a.state='已完成'  and  a.seller_id=" + sellerId + " and  date(a.orderdate)='" + format.format(time) + "'").uniqueResult();

        return   sum;
    }

    @Override
    public Double getMoneyOfOneMonthBySeller(int year, int month, int sellerId) {
        Double sum = (Double) getSession().createSQLQuery("select sum(a.total) from t_order as a  where  a.state='已完成'  and  a.seller_id=" + sellerId + " and  year(a.orderdate)='" + year + "' and month(a.orderdate)='"+month+"'").uniqueResult();
        return   sum;
    }
    @Override
    public Pager<Order> loadBySellerId(int id) {
        return this.find("from Order where seller_id = '" + id + "'");
    }

    @Override
    public Pager<Order> loadByUserId(int id) {
        return this.find("from Order where user_id = '" + id + "'");
    }

    @Override
    public Pager<Order> findOrder() {
        return this.find("from Order");
    }

    @Override
    public Pager<Order> findOrderByState(String state) {
        return this.find("from Order where state = '" + state + "'");
    }

    @Override
    public Pager<Order> findBySellState(int seller, String state) {
        return this.find("from Order where seller_id = '" + seller + "' and state = '" + state + "'");
    }

    @Override
    public Pager<Order> findOrderById(int id) {
        return this.find("from Order where id = '"+ id +"'");
    }

    @Override
    public Pager<Order> findByUserState(int user, String state) {
        return this.find("from Order where user_id = '" + user + "' and state = '" + state + "'");
    }
    public Pager<Order> findByState(int sellerId, String s1, String s2) {
        return this.find("from Order where seller_id = '" + sellerId + "' and state in ('" +s1 + "','" + s2 + "')");
    }


}
